import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Baidu {
    static Init init = new Init();
    static Scanner sc = new Scanner(System.in);
    public static void Spy() throws IOException {
        System.out.println("目标百度,全力爬取");
        init.initUnSecureTSL();
        System.out.println("请输入要搜索的内容:");
        String urls=java.net.URLEncoder.encode(sc.next(), StandardCharsets.UTF_8);
        Connection connect = Jsoup.connect(Main.urls[0]+"s?wd="+urls );
//        Connection connect=null;
//        int count=0;
//        for (Map.Entry<String,Integer> entry : Init.proxys.entrySet()){
//            count++;
//            System.out.println("设置代理:"+entry.getKey());
//            connect=connect0.proxy(entry.getKey(),entry.getValue());
//            try{
//                connect.get();
//            }catch(Exception e){
//                System.out.println("代理失效,重新更换代理");
//                if (count==Init.proxys.size()){
//                    System.out.println("所有代理失效,请自行更换代理");
//                    System.exit(0);
//                }
//                else{
//                    continue;
//                }
//            }
//            System.out.println("代理设置成功");
//            break;
//        }
        Connection conheader;
        int i = Init.userAgents.size();
        Document document = null;
        HashMap<String,String> header = null;
        while(true){
            if (Init.userAgents.isEmpty()){
                System.out.println(i+"条用户标识已耗尽");
                break;
            }
            try{
                header=init.Builder(Main.urls[0]);
                conheader = connect.headers(header);
                document=conheader.get();
            }catch(HttpStatusException e){
                System.out.println("用户标识失效,重新更换标识");
                continue;
            }
            System.out.println("已成功连接百度");
            break;
        }
        Elements elements = document.select("div#content_left");
        Elements elements_title = elements.select("h3>a");
        Elements elements_content=elements.select("div.c-abstract");
        show(elements_title,elements_content);
        jump(0,urls,header);
    }
    static void show(Elements elements_title,Elements elements_content){
        int i=0;
        if (elements_title.isEmpty()){
            System.out.println("未搜索到内容");
        }
        else{
            for (Element element : elements_content) {
                while (i < elements_title.size()) {
                    String s = elements_title.get(i).text();
                    i++;
                    if (!s.contains("- 视频大全 - 高清在线观看")) {
                        System.out.println(s);
                        break;
                    }
                }
                System.out.println(element.text()+"\n");
            }
        }
    }
    static void jump(int page,String urls,HashMap<String,String> header) throws IOException {
        System.out.println("请输入要跳转到第几个页面,next下一页,其他退出");
        boolean boo=true;
        String page_text=sc.next();
        if (page_text.equals("next")){
            page+=10;
        }
        else{
            try{
                page=(Integer.parseInt(page_text)-1)*10;
            }catch(Exception e){
                boo=false;
                System.out.println("返回主页面");
            }
        }
        if (boo){
            Connection connect = Jsoup.connect(Main.urls[0]+"s?wd="+urls+"&pn="+page);
            Connection conheader = connect.headers(header);
            Document document=conheader.get();
            Elements elements = document.select("div#content_left");
            Elements elements_title = elements.select("h3>a");
            Elements elements_content=elements.select("div.c-abstract");
            show(elements_title,elements_content);
            jump(page,urls,header);
        }
    }
}
